import org.junit.Test;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.Properties;

/**
 * @author <a href='mailto:maruichao52@gmail.com'>MRC</a>
 * @since 2021/2/23
 */
public class MailTest {

    public static void main(String[] args) throws MessagingException {

        Properties prop = new Properties();

        prop.put("mail.transport.protocol", "smtp");
        prop.put("mail.smtp.host", "smtp.exmail.qq.com");
        prop.put("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(prop);

        MimeMessage message = new MimeMessage(session);

        message.setSubject("title");
        message.setSentDate(new Date());
        message.setSender(new InternetAddress("maruichao@aishua.cn"));
        message.setRecipients(Message.RecipientType.TO, "1763907575@qq.com");

        message.setText("Hello World");
        message.saveChanges();

        Transport trans = session.getTransport();
        trans.connect("maruichao@aishua.cn", "DCFeM3LyumDGy7Ty");
        trans.sendMessage(message, message.getAllRecipients());
        trans.close();
    }

    @Test
    public void sendFile() throws Exception {

        Properties prop = new Properties();

        prop.put("mail.transport.protocol", "smtp");
        prop.put("mail.smtp.host", "smtp.exmail.qq.com");
        prop.put("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(prop);

        MimeMessage message = new MimeMessage(session);

        InternetAddress sender = new InternetAddress("maruichao@aishua.cn");
        sender.setPersonal("连银-马瑞朝","UTF-8");

        message.setSubject("测试邮件");
        message.setSentDate(new Date());
        message.setSender(sender);
        message.setRecipients(Message.RecipientType.TO, "1763907575@qq.com");
        message.setFrom(sender);

        MimeMultipart mixed = new MimeMultipart("mixed");

        MimeBodyPart other = new MimeBodyPart();
        MimeBodyPart file = new MimeBodyPart();

        // 加载附件
        String path = this.getClass().getResource("test.txt").getPath();
        InputStream inputStream = new FileInputStream(path);

        DataSource dataSource = new ByteArrayDataSource(inputStream,"application/octet-stream");

        DataHandler dataHandler = new DataHandler(dataSource);

        file.setFileName("test.txt");
        file.setDataHandler(dataHandler);

        MimeMultipart related = new MimeMultipart("related");
        // 组合资源
        MimeBodyPart content = new MimeBodyPart();
        MimeBodyPart resource = new MimeBodyPart();

        // 图片资源
        URL picture = this.getClass().getResource("test.png");
        resource.setDataHandler(new DataHandler(picture));
        resource.setContentID("mm.png");

        // 设置内容
        content.setContent("<img src='cid:mm.png'/>  好哈哈！", "text/html;charset=UTF-8");

        related.addBodyPart(content);
        related.addBodyPart(resource);

        other.setContent(related);

        mixed.addBodyPart(other);
        mixed.addBodyPart(file);

        message.setContent(mixed);
        message.saveChanges();

        Transport trans = session.getTransport();
        trans.connect("maruichao@aishua.cn", "xxxxxx");
        trans.sendMessage(message, message.getAllRecipients());
        trans.close();
    }


}
